package com.ywl.controller;


import com.ywl.common.R;
import com.ywl.dto.OrdersDto;
import com.ywl.entity.HouseDetail;
import com.ywl.entity.Orders;
import com.ywl.service.HouseDetailService;
import com.ywl.service.OrderService;
import com.ywl.utils.MyStringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ywl
 * @since 2022-08-26
 */
@RestController
@Api(tags = "订单相关接口")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private HouseDetailService houseDetailService;

    /**
     * 获取订单List
     * @param phone
     * @return
     */
    @ApiOperation("用户订单列表接口")
    @GetMapping("/order")
    /*@Cacheable(value = "orderCache",
            key = "'order_'+#phone",
            unless = "#result == null or #result.size() == 0"
    )*/
    public List<OrdersDto> getOrderList(String phone){
        log.info(phone);
        List<OrdersDto> orders = orderService.getByPhone(phone);
        return orders;
    }

    @ApiOperation("预计倒计时接口")
    @GetMapping("/buytime")
    public R<String> getTime(@RequestParam("id") Long detailId){
//        log.info("detailId={}",detailId);
        long buyTime = System.currentTimeMillis();
        return R.success("预计时间",buyTime);
    }

    @ApiOperation("商品预定接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user",value = "用户手机号",required = true),
            @ApiImplicitParam(name = "id",value = "房屋id",required = true)
    })
    @GetMapping("/buyaction")
    /*@CacheEvict(value = "orderCache",allEntries = true) //清除缓存数据*/
    @Transactional
    public R<String> buyAction(@RequestParam("user") String phone,@RequestParam("id")Long detailId){
        log.info("userPhone={}",phone);
        log.info("detailId={}",detailId);
        //保存订单信息
        Orders order = new Orders();
        order.setDetailId(detailId);//房屋id
        order.setPhone(phone);//手机号
        LocalDateTime now = LocalDateTime.now();
        order.setCreateTime(now);//预定时间
        order.setCommentState(0);//是否评论
        order.setStatus(1);//状态

        //查询房屋信息,并进行图片的处理
        HouseDetail houseDetail = houseDetailService.getById(detailId);//获取房屋信息
        order.setImg(houseDetail.getImg());//设置订单图片

        houseDetailService.updateById(houseDetail);
        //保存订单
        boolean save = orderService.save(order);
        long time = now.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return save ? R.success("预订成功",time) : R.error("手太慢，已经被抢订，预订失败");
    }

}

